using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace StockControlSystem
{
    public partial class Computer : Form
    {
        private Controller m_controller;

        public Computer()
        {
            InitializeComponent();
            m_controller = new Controller();
        }

        public string StockItemKeyToOrder
        {
            get
            {
                return tbOrderItemKey.Text.Trim();
            }
        }

        public void ClearStockItemKeyToOrder()
        {
            tbOrderItemKey.Text = string.Empty;
        }

        public string NumStockTiemsToOrder
        {
            get
            {
                return tbNumItemsToOrder.Text.Trim();
            }
        }

        public void ClearNumStockItemsToOrder()
        {
            tbNumItemsToOrder.Text = string.Empty;
        }

        public string StockItemKeyToRestock
        {
            get
            {
                return tbRestockItemKey.Text.Trim();
            }
        }

        public void ClearStockItemKeyToRestock()
        {
            tbRestockItemKey.Text = string.Empty;
        }


        private void Computer_Load(object sender, EventArgs e)
        {   
            // This handler is for initialization
        }


        private void bnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void bnListStockBelowTH_Click(object sender, EventArgs e)
        {
        }

        private void bnListAllStock_Click(object sender, EventArgs e)
        {
        }

        private void bnListAllSuppliers_Click(object sender, EventArgs e)
        {
            ListDialog listDialog = new ListDialog();
            string[] allSuppliersDescription = m_controller.GetAllSuppliersDescrition();
            listDialog.AddDisplayItems(allSuppliersDescription);
            listDialog.ShowDialog();
        }

        private void bnOrder_Click(object sender, EventArgs e)
        {
        }

        private void bnListOutstandingOrders_Click(object sender, EventArgs e)
        {
            // the following lines are to instruct you how to use ListDialog.
            // You should replace the following code with yours
            ListDialog listDialog = new ListDialog();
            listDialog.AddDisplayItems("This is a test", "You can write any number of arguments,",
                "to pass to AddDisplayItems");
            object[] displayObjects= {
                                         "You can also pass object[],",
                                         "which contains any number of objects"
                                     };
            listDialog.AddDisplayItems(displayObjects);
            listDialog.AddDisplayItems("You can call AddDisplayItems as many times as you want");                           
            listDialog.AddDisplayItems("Each invocation adds lines to be displayed");
            listDialog.AddDisplayItems("By the way, did you know that ToString() of string returns its string component");
            listDialog.ShowDialog();
        }

        private void bnRestock_Click(object sender, EventArgs e)
        {
        }

        private void bnLoadStock_Click(object sender, EventArgs e)
        {
            // load stock file
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "StockItems|*.sti";
            openFileDialog.InitialDirectory = Application.StartupPath;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    TextReader trs = new StreamReader(openFileDialog.FileName);
                    string s;
                    string[] properties = new string[4];
                    char[] spliter = { '&' };
                    while ((s = trs.ReadLine()) != null)
                    {
                        properties = s.Split(spliter);
                        // you have all 4 strings in properties[]
                        // to extract a string, you may want to do the following
                        // string s = properties[i].trim();  
                        // to be honest, I am not sure if trim() is necessary. But I call it anyways
                        int key = int.Parse(properties[0].Trim());
                        string name = properties[1].Trim();
                        string barCode = properties[2].Trim();
                        double price = double.Parse(properties[3].Trim());
                        int threshould = int.Parse(properties[4].Trim());
                        int numberofIterms = int.Parse(properties[5].Trim());
                        Stock sto = new Stock(key, name, barCode, price, threshould, numberofIterms);
                        int supKey = int.Parse(properties[6].Trim());
                        m_controller.AddStock(sto, supKey);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception is caught (StockItems)" + ex.Message + "\n" + ex.StackTrace);
                }
            }
            bnListStockBelowTH.Enabled = bnListAllStock.Enabled = bnListAllSuppliers.Enabled = true;
            bnListOutstandingOrders.Enabled = bnOrder.Enabled = bnRestock.Enabled = true;
            bnLoadSuppliers.Enabled = bnLoadStock.Enabled = false;

            // I display Till here (using Show() since Till is a modeless dialog box

            Till till = new Till();
            till.Show();
        }

        private void bnLoadSuppliers_Click(object sender, EventArgs e)
        {
            // load supplier file here
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Suppliers|*.spl";
            openFileDialog.InitialDirectory = Application.StartupPath;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    TextReader trs = new StreamReader(openFileDialog.FileName);
                    string s;
                    string[] properties = new string[4];
                    char[] spliter = { '&' };
                    while ((s = trs.ReadLine()) != null)
                    {
                        properties = s.Split(spliter);
                        // you have all 4 strings in properties[]
                        // to extract a string, you may want to do the following
                        // string s = properties[i].trim();  
                        // to be honest, I am not sure if trim() is necessary. But I call it anyways
                        int key = int.Parse(properties[0].Trim());
                        string name = properties[1].Trim();
                        string address = properties[2].Trim();
                        string telphoneNumber = properties[3].Trim();
                        Supplier sup = new Supplier(key, name, address, telphoneNumber);
                        m_controller.AddSupplier(sup);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Exception is caught (LoadSuppliers)");
                }
            }
            bnLoadStock.Enabled = true;
        }
    }
}
